/*
 * @Date: 2022-02-25 16:37:23
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2023-11-28 11:29:59
 * @FilePath: \server\system\service\SysService\role.go
 */

package SysService

import (
	"go-easy-dashboard/system/core/db"
	"go-easy-dashboard/system/core/request"
	"go-easy-dashboard/system/model/CommonModel"
	"go-easy-dashboard/system/model/DbModel"
	"gorm.io/gorm"

	"go-easy-dashboard/system/model/RequestModel"
)

type RoleService struct{}

//新增
func (RoleService *RoleService) Add(info *DbModel.SysRole) (err error) {
	result := db.Instance().Create(info)
	return result.Error
}

//修改
func (RoleService *RoleService) Update(info *DbModel.SysRole) (err error) {
	var user DbModel.SysRole
	if err := db.Instance().First(&user, info.Name).Error; err != nil {
		return err
	}
	//忽略字段
	return db.Instance().Omit("CreatedAt", "Password").Save(info).Error
}

//分页列表
func (RoleService *RoleService) List(info RequestModel.SysRoleSearch) (err error, list interface{}, total int64) {

	db := db.Instance().Model(&DbModel.SysRole{})
	//动态查询条件
	request.GenWhere(info, db)
	var userList []DbModel.SysRole
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Find(&userList).Error
	return err, userList, total
}

//获取已经具备的权限
func (RoleService *RoleService) Permissions(id uint) (err error, list interface{}) {
	var dataList []DbModel.SysRolePermission
	err = db.Instance().Preload("Permission").Where("role_id = ?", id).Find(&dataList).Error
	return err, dataList
}

//分配权限
func (RoleService *RoleService) SetPermissions(r RequestModel.SysSetPermissions) (err error) {
	var dataList []DbModel.SysRolePermission
	for _, v := range r.CheckedKeys {
		dataList = append(dataList, DbModel.SysRolePermission{RoleName: r.Name, PermissionName: v, Checked: 1})
	}
	for _, v := range r.HalfCheckedKeys {
		dataList = append(dataList, DbModel.SysRolePermission{RoleName: r.Name, PermissionName: v, Checked: 0})
	}

	return db.Instance().Transaction(func(tx *gorm.DB) error {
		tx.Exec("set autocommit=0")
		// 使用 'tx' 而不是 'db'
		if err := tx.Where("role_name = ?", r.Name).Delete(&DbModel.SysRolePermission{}).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		if len(dataList) > 0 {
			if err := tx.Create(&dataList).Error; err != nil {
				return err
			}
		}
		// 返回 nil 提交事务
		return nil
	})
}

//删除
func (RoleService *RoleService) Remove(info CommonModel.Ids) (err error) {

	//关联删除 无法实现 ，未知原因，被迫手动二次删除
	// var data DbModel.SysRole
	// result := db.Instance().Select(clause.Associations).Where("id in ? ", info.Ids).Delete(&data)
	// return result.Error
	return db.Instance().Transaction(func(tx *gorm.DB) error {
		tx.Exec("set autocommit=0")
		// 使用 'tx' 而不是 'db'
		if err := tx.Where("role_id in ?", info.Ids).Delete(&DbModel.SysRolePermission{}).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		if err := tx.Delete(&DbModel.SysRole{}, info.Ids).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}
		// 返回 nil 提交事务
		return nil
	})
}
